package com.burgeon.backend.EnterpriseWeChat.rest.Brand;

import com.burgeon.backend.EnterpriseWeChat.entity.Product;
import com.burgeon.backend.EnterpriseWeChat.entity.StoreInventory;
import com.burgeon.backend.EnterpriseWeChat.rest.base.RESTBase;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class getProductInventory extends RESTBase {
    private final static String RANGE = "20";//分页数量
    private String ip;
    private String account;
    private String password;

    public getProductInventory(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     *
     * @return
     */

    public ArrayList<StoreInventory> getData(String productName) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(productName), url);
        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception(new JSONArray(str).getJSONObject(0).getString("message"));
        }

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        ArrayList<StoreInventory> storeInventories= new ArrayList<>();
        if(jsonArray.length()>0){
            for (int i = 0; i <jsonArray.length(); i++) {
                StoreInventory storeInventory = new StoreInventory();
                storeInventory.setStoreIdName(jsonArray.getJSONArray(i).getString(0));
                storeInventory.setInventory(jsonArray.getJSONArray(i).getString(1));
                storeInventory.setLongitude(jsonArray.getJSONArray(i).getString(2));
                storeInventory.setLatitude(jsonArray.getJSONArray(i).getString(3));
                storeInventory.setBrandName(jsonArray.getJSONArray(i).getString(4));

               storeInventories.add(storeInventory);
            }
        }

        return storeInventories;
    }

    private MultiValueMap Data(String productName) {
        HashMap params = new HashMap();
        ArrayList colums = new ArrayList();//查询字段

        colums.add("'C_STORE_ID;NAME'");
        colums.add("QTYCAN");//
        colums.add("'C_STORE_ID;LONGITUDE'");//经度
        colums.add("'C_STORE_ID;LATITUDE'");//纬度
        colums.add("'M_DIM1_ID;ATTRIBNAME'");//商品的品牌名称

        params.put("columns", colums);
        params.put("table", "APP_STORAGEPRO_STORE");//表名
        params.put("count", "true");
        HashMap orderby = new HashMap();

        orderby.put("column", "QTYCAN");
        orderby.put("asc", "false");
        ArrayList list = new ArrayList();
        list.add(orderby);

        params.put("orderby", list);

        HashMap paramss = new HashMap();

        paramss.put("column","'M_PRODUCT_ID;NAME'");
        String a = "'"+productName+"'";
        paramss.put("condition",a);


        params.put("params", paramss);
//        System.err.println(paramss);
        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        getProductInventory g = new getProductInventory("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        try {
            System.err.println(g.getData("BDN4RD585"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
